/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wap.rockvilletech.util;

import java.util.ArrayList;
import java.util.Vector;

/**
 *
 * @author Usman
 */
public class VectorUtil {
    
    /**
     * appends Vector v2 to Vector v1 
     * @param v1
     * @param v2
     * @return Vector v1
     */
    public static Vector v_Combine(Vector v1, Vector v2) {
        return v_Combine(v1, v2, 99, 99, false);
    }
    /**
     * appends Vector v2 to v1.
     * removes duplicates from the resultant vector(v1) on the basis of the column (colID)
     * @param v1
     * @param v2
     * @param colID
     * @param removeDuplicate
     * @return
     */
    public static Vector v_Combine(Vector v1, Vector v2, int colID, boolean removeDuplicate) {
        return v_Combine(v1, v2, colID, 99, removeDuplicate);
    }
    /**
     * appends Vector v2 to Vector v1.
     * loops throught the resultant Vector v1 and adds the column (colNumToAdd),
     * if the any of the two vectors have same ID.
     * To skip column addition, pass 99 for both colID and colNumToAdd.
     * @param v1
     * @param v2
     * @param colID The position of ID column in the vector, both vectors must have the same column as ID column.
     * @param colNumToAdd The column to add, both vectors must have the same number column to be added.
     * @param removeDuplicate Weather to remove the duplicate entries from the vector or not
     * @return Vector v1 removeDuplicate
     * @note Is used by removeDuplicate
     */
    public static Vector v_Combine(Vector v1, Vector v2, int colID, int colNumToAdd, boolean removeDuplicate){
        long 
                $Num1 = 0, $Num2 = 0;
        String
                $ID1 = "", $ID2 = "";
        
        // Combine the two vectors if v2 is not null
        if (v2 != null) {
            for(int i = 0; i < v2.size(); i++) {
                v1.add(v2.get(i));
            }
        }
        
        // Perform the Required operation
        int $vSize = v1.size();
        if (colID != 99) {
            for(int i = 0; i < $vSize-1; i++) {
                $ID1 = ((Vector)v1.get(i)).get(colID).toString().trim();
                for (int j = i+1; j < $vSize; j++) {
                    $ID2 = ((Vector)v1.get(j)).get(colID).toString().trim();
                    if( $ID1.equalsIgnoreCase( $ID2 ) ) {
                        if (colNumToAdd != 99) {
                            try {
                                $Num1 = Long.parseLong(((Vector)v1.get(i)).get(colNumToAdd).toString());
                                $Num2 = Long.parseLong(((Vector)v1.get(j)).get(colNumToAdd).toString());
                                ((Vector)(v1.get(i))).set(colNumToAdd, $Num1+$Num2);
                            } catch(Exception ex) {
                                
                            }
                        }
                        if (removeDuplicate) {
                            v1.removeElementAt(j);
                            j -= 1;
                            $vSize = v1.size();
                        }
                        continue;
                    }
                }
            }
        }
        return v1;
    }
    
    /**
     * Converts a vector to String[2], String values are delimited by comma(,).
     * Currently the vector must have two columns
     * @param v
     * @return
     */
    public static String[] vectorToString(Vector v) {
        return vectorToString(v, ",");
    }
    /**
     * Converts a vector to String[2], String values are delimited by the delimiter passed.
     * Currently the vector must have two columns
     * @param v
     * @param Delimiter
     * @return String[]
     */
    public static String[] vectorToString(Vector v, String Delimiter) {
        String[] $vStr = {"",""};
        for(int x=0; x<v.size(); x++){
            try {
                if(x == 0) {
                    $vStr[0] = ((Vector)v.get(x)).get(0).toString().trim();
                    $vStr[1] = ((Vector)v.get(x)).get(1).toString().trim();
                }
                else {
                    $vStr[0] = $vStr[0] + Delimiter + ((Vector)v.get(x)).get(0).toString().trim();
                    $vStr[1] = $vStr[1] + Delimiter + ((Vector)v.get(x)).get(1).toString().trim();
                }
            } catch(Exception ex) {
                $vStr = null;
            }
        }
        return $vStr;
    }
    public static String[] vectorToString(Vector v, int colToReturn) {
        String[] $str;
        int $size = v.size();
        
        $str = new String[$size];
        for (int i = 0; i < $size; i++) {
            try {
                if ((v.get(i) instanceof Vector)) {
                    $str[i] = ((Vector)v.get(i)).get(colToReturn).toString();
                }
                else {
                    $str[i] = v.get(i).toString();
                }
            }
            catch (Exception ex) {
                $str[i] = " ";
            }
        }
        
        return $str;
    }
    
    /**
     * Returns the sum of the first Column of the Vector v
     * @param v
     * @return long sum of the values of the First Column
     */
    public static long addVectorColumn(Vector v) {
        return addVectorColumn(v, 0);
    }
    /**
     * Returns the sum of the Column (colToAdd) of the Vector v
     * @param v
     * @param colToAdd
     * @return long sum of the values of the Column (colToAdd)
     */
    public static long addVectorColumn(Vector v, int colToAdd) {
        long $sum = 0;
        
        for(int i = 0; i < v.size(); i++) {
            try {
                 $sum += Long.parseLong(((Vector)v.get(i)).get(colToAdd).toString());
            } catch(Exception ex) {
                
            }
        }
        
        return $sum;
    }

    /**
     * Sorts the given Vector v on First Column(supposed to be the ID column).
     * @param v Vector to be sorted
     * @return the sorted vector
     */
    public static Vector sort(Vector v) {
        return sort(v, 0);
    }
    /**
     * Sorts the given Vector v on the basis of the column colToSortOn.
     * @param v Vector to be sorted
     * @param colToSortOn The column number on which the vector will be sorted, it has to be a numeric column
     * @return the sorted vector
     */
    public static Vector sort(Vector v, int colToSortOn) {
        
        int $vSize = v.size();
        long $long1 = 0, $long2 = 0;
        Vector $temp = null;
        
        for (int i = 0; i < $vSize-1; i++) {
            $long1 = Long.parseLong(((Vector)v.get(i)).get(colToSortOn).toString());
            for (int j = i+1; j < $vSize; j++) {
                $long2 = Long.parseLong(((Vector)v.get(j)).get(colToSortOn).toString());
                if ($long2 > $long1) {
                    $temp = (Vector)v.get(i);
                    v.set(i, v.get(j));
                    v.set(j, $temp);
                    
                    // Line added to refresh the 
                    $long1 = Long.parseLong(((Vector)v.get(i)).get(colToSortOn).toString());
                }
            }
        }
        $temp = null;
        return v;
    }
    /**
     * Not Yet Implemented
     * @param v
     * @param colToSortOn
     * @return null
     */
    public static Vector sort(Vector v, String colToSortOn) {
        return null;
    }
    
    /**
     * Removes duplicate entries from the vector passed.
     * @param v Vector to be sorted
     * @return Vector, from which duplicates have been removed on the basis of the first column
     */
    public static Vector removeDuplicate(Vector v) {
        return removeDuplicate(v, 0);
    }
    /**
     * Removes duplicate entries from the vector passed.
     * @param v Vector to be sorted
     * @param colToCheck Column that will act as a key
     * @return Vector, from which duplicates have been removed on the basis of the key (colToCheck)
     */
    public static Vector removeDuplicate(Vector v, int colToCheck) {
        return v_Combine(v, null, colToCheck, 99, true);
    }
    
    public static boolean isVector(Object objt) {
        boolean res = false;
        Vector $v;
        try {
            $v = (Vector)(objt);
            res = true;
        }
        catch (ClassCastException ccex) {
            res = false;
            System.out.println( ccex.getCause() );
        }
        catch (Exception ex) {
            res = false;
            System.out.println( ex.getCause() );
        }
        $v = null;
        return res;
    }
    
    public static ArrayList toArrayList(Vector v) {

        ArrayList arrList = new ArrayList();
        Vector tmp ;
        
        try {
            if (v == null) {
                return arrList;
            }
            
            for (int i = 0; i < v.size(); i++) {
                tmp = (Vector) v.get(i);
                for (int j = 0; j < tmp.size(); j++) {
                    if (tmp.get(j) != null) {
                        String value = tmp.get(j).toString();
                        if (value != null && !value.equalsIgnoreCase("")) { }
                        else {
                            value = "0";
                        }
                        arrList.add(value);
                    } else {
                        arrList.add("0");
                    }
                }
            }
        }
        catch (Exception exc) {
            exc.printStackTrace();
        }
        return arrList;
    }
    
}
